//show message 
function showMsg(dom,msg){
   $(dom).text(msg).show();
   setTimeout(function(){
       $(dom).hide();
   },2000)
}

//navbar toggle show
$('.navbar-toggler').click(function(){
    $('#navbar').toggle();
});

//get query 
function getUrlParam(id) {
    var regExp = new RegExp('([?]|&)' + id+ '=([^&]*)(&|$)');
    var result = window.location.href.match(regExp);
    if (result) {
        return result[2];
    } else {
        return null;
    }

}
//set url query
 function UpdateUrlParam(name, val) {
    var thisURL = document.location.href;
    if (thisURL.indexOf(name+'=') > 0) {
        var v = getUrlParam(name);
        if (v != null) {
            thisURL = thisURL.replace(name + '=' + v, name + '=' + val);
        }else {
            thisURL = thisURL.replace(name + '=', name + '=' + val);
        }
        
    }else {
     
        if (thisURL.indexOf("?") > 0) {
            thisURL = thisURL + "&" + name + "=" + val;
        }else {
            thisURL = thisURL + "?" + name + "=" + val;
        }
    }
    
    location.href = thisURL;
};

//upvote question
$('body').on('click','.small-upvote',function(){
    var qid=$(this).data('qid');
    var dom=$(this);
   if($('.user-info').length){
       $.ajax({
           url:'/upvoteQuestion',
           type:'GET',
           data:{
               qid:qid
           }
       }).done(function(res){
           if(res.success){
               var num= parseInt(dom.find('.upvote-num').text());
              if(res.code=='+1'){
                dom.find('.icon-upvote').addClass('red')
                dom.find('.upvote-num').text(num+1);
              }else{
                dom.find('.icon-upvote').removeClass('red')
                if(num-1<0){
                    num=0
                }else{
                    num=num-1
                }
                dom.find('.upvote-num').text(num);
              }
           }else{
               alert('upvote fail');
           }
       }).fail(function(){
           alert('server error');
       });
   }else{
       alert('please sign in');
   }
});


//create answer
$('body').on('click','.btn-answer-submit',function(){
    var content=$(this).closest('.answer-form').find('[name="answer"]').val().trim();
    var dom=$(this);
    if(!content){
        showMsg($(this).closest('.answer-form').find('.error-answer-info'),'Please input answer');
        return
    }
    var qid=$(this).data('qid');
    $.ajax({
        url:'/answer',
        type:'POST',
        data:{
            qid,
            content
        }
    }).done(function(res){
        if(res.success){
            if(location.href.indexOf('/question')>-1){
                window.location.href='/';
            }else{
                var answer=res.answer;
                var t=tpl.replace('{{uname}}',answer.uname);
                t=t.replace('{{time}}',answer.time);
                t=t.replace('{{content}}',answer.content);
                dom.closest('.answer-form').before(t);
                dom.closest('.answer-form').find('[name="answer"]').val('')
                var num=dom.closest('.question-item').find('.answer-num').text();
                num=parseInt(num);
                dom.closest('.question-item').find('.answer-num').text(num+1);
            }
        }else{
          showMsg(dom.closest('.answer-form').find('.error-answer-info'),res.msg);
        }

    }).fail(function(xhr){
        showMsg(dom.closest('.answer-form').find('.error-answer-info'),'Post answer fail');
        console.log(xhr);
    })

});